var app = getApp()
Page({
    data: {
        code: 0,
        iszan: 0,
        likes: 0,
        showImage: false
    },

    onLoad(options) {
        var that = this
        const userinfo = tt.getStorageSync('userinfo')
        if (!userinfo) {
            app.wxlogin().then(function (res) {
                that.setData({
                    userinfo: res,
                    id: options.id || 0
                },() => {
                    that.getdata()
                })
            })
        } else {
            that.setData({
                userinfo,
                id: options.id || 0
            },() => {
                that.getdata()
            })
        }
    },

    getdata() {
        var that = this
        const { uid,openid } = that.data.userinfo
        const { id } = that.data
        const data = { uid,openid,id }
        //console.log(data)
        app.api_request('?c=coach_detail','GET',data).then(function (res) {
            if (res) {
                that.setData({
                    code: 1,
                    pagedata: res,
                    coachid: res.coachid,
                    likes: res.likes
                },() => {
                    that.getlist()
                })
                tt.setNavigationBarTitle({
                    title: res.appTitle
                })
            }
        })
    },

    getlist() {
        var that = this
        const { id } = that.data
        const data = { id }
        app.api_request('?c=coach_comment','GET',data).then(function (res) {
            if (res) {
                that.setData({
                    pagelist: res
                })
            }
        })
    },

    likes() {
        var that = this
        const { uid,openid } = that.data.userinfo
        const { id,likes } = that.data
        const data = {}
        data.m_uid = uid
        data.m_openid = openid
        data.m_id = id
        //console.log(data)
        app.api_request('?c=coach_likes','POST',data).then(function (res) {
            tt.showToast({
                icon: 'none',
                title: res.tipmsg,
                success: function () {
                    if (res.state == 1) {
                        that.setData({
                            iszan: 1,
                            likes: likes + 1
                        })
                    }
                }
            })
        })
    },

    callphone(e) {
        const { phone } = e.currentTarget.dataset
        tt.makePhoneCall({
            phoneNumber: phone,
            success(res) {
                console.log("调用成功", res.errMsg)
            },
            fail(res) {
                console.log("调用失败", res.errMsg)
            }
        })
    },

    showImage(e) {
        this.setData({
            showImage: true
        })
    },

    closeAny() {
        this.setData({
            showImage: false
        })
    },

    onShareAppMessage: function () {
        var that = this
        return {
            title: that.data.pagedata.appTitle,
            desc: that.data.pagedata.appDesc,
            path: '/pages/coach/detail?id=' + that.data.pagedata.id
        }
    }
})